package cn.lanqiao.active;

public class TestThread {
    public static int i = 10;//共享数据
    public static Object lock = new Object();

    public static void main(String[] args) {

        //匿名内部类标准写法
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (TestThread.lock) {
                        if (TestThread.i > 0) {
                            try {
                                TestThread.lock.wait(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + " -- " + TestThread.i--);
                        }
                    }
                }
            }
        }).start();
        //匿名内部类的lambda表达式写法
        new Thread(() -> {
            while (true) {
                synchronized (TestThread.lock) {
                    if (TestThread.i > 0) {
                        System.out.println(Thread.currentThread().getName() + " -- " + TestThread.i--);
                    }
                }
            }
        }).start();

    }

}



